<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/23
 * Time: 17:44
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Session;

class Login extends Controller
{
    public function login()
    {
        if(session('admin_info')){
            $this->redirect('/admin/index/index');
        }
        return $this->fetch();
    }

    /**
     * 登录
    */
    public function loginIn()
    {
        if(request()->isPost()){
            $param = input("post.");

            if(empty($param['username'])){
                return json(['code'=>'-1','data'=>"",'msg'=>'用户名不能为空啦']);
            }
            if(empty($param['password'])){
                return json(['code'=>'-1','data'=>"",'msg'=>'呀呀呀!密码为空怎么能登录呢？']);
            }

            $param['password'] = md5($param['password'] . config('salt'));
            $login_info = Db::table("z_user")->where(['username' =>$param['username'],'password' => $param['password']])->find();
            if(empty($login_info)){
                return json(['code'=>'-1','data'=>"",'msg'=>'用户名或者密码错误']);
            }
            if($login_info['disabled'] != '1'){
                return json(['code'=>'-1','data'=>"",'msg'=>'账号处于禁用状态,请联系网站管理员']);
            }

            Db::table('z_user')->where('username',$param['username'] )->setInc('login_num');
            
            session('admin_info',$login_info);
            return json(['code'=>'1','data'=>url('index/index'),'msg'=>'登录成功']);
        }
    }
    /**
     * 空操作
     */
    public  function  _empty(){
        return $this->fetch('Error/404');
        // $this->error('空操作，正在跳转','Index/main');
    }

    /**
     * 退出登录
     */
    public function outLogin()
    {
        if(request()->isPost()){
//            session(null);
            session_start();
            session_destroy();
            return json(['code'=>'1','data'=>url('login/login'),'msg'=>'退出成功']);
        }
    }

}